from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
# main = Blueprint("main", __name__)
stuinfo = Api()
class Stuinfo1(Resource):
    def get(self):
        stuinfo = StudentInfo.query.all()
        return jsonify({"stuinfo":list(map(convert_list2, stuinfo))})
    def post(self):
        stuinfo_add_one = StudentInfo(
            request.json.get('name'),
            request.json.get('sex'),
            request.json.get('id_card'),
            request.json.get('local'),
            request.json.get('wechat'),
            request.json.get('work')
        )
        db.session.add(stuinfo_add_one)
        db.session.commit()
        return jsonify({'stuinfo_add_one': convert_list2(stuinfo_add_one)})
stuinfo .add_resource(Stuinfo1,"/stuinfo1/")

class Stuinfo2(Resource):
    def get(self,uid):
        stuinfo_one = StudentInfo.query.filter_by(id=uid).first()
        if stuinfo_one is None:
            abort(404)
        return jsonify({'stuinfo_one': convert_list(stuinfo_one)})
    def put(self,uid):
        stuinfo_update = StudentInfo.query.get(uid)
        stuinfo_update.name = request.json.get('name')
        stuinfo_update.sex = request.json.get('sex')
        stuinfo_update.id_card = request.json.get('id_card')
        stuinfo_update.local = request.json.get('local')
        stuinfo_update.wechat = request.json.get('wechat')
        stuinfo_update.work = request.json.get('work')

        db.session.add(stuinfo_update)
        db.session.commit()
        return jsonify({'stuinfo_update': convert_list2(stuinfo_update)})
stuinfo .add_resource(Stuinfo2, "/stuinfo2/<int:uid>/")